package cn.wu.reggie.controller;

import cn.wu.reggie.common.R;
import cn.wu.reggie.entity.Category;
import cn.wu.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;


    // 对应不上前端的page数据 用注解
    @GetMapping("page")
    public R findByPage(@RequestParam("page") Integer pageNum,Integer pageSize){
        // 调用service完成分页查询操作
        return categoryService.findByPage(pageNum,pageSize);
    }

    @PostMapping
    public R save(@RequestBody Category category, HttpSession session){
        // 输入category封装的用户id
        Long userId = (Long) session.getAttribute("employee");
        category.setUpdateUser(userId);
        category.setCreateUser(userId);

        // 调用service完成保存
        return categoryService.save(category);

    }

    @PutMapping
    public R update(@RequestBody Category category, HttpSession session){
        // 给category封装更新用户id
        Long userId = (Long) session.getAttribute("employee");
        category.setUpdateUser(userId);

        // 调用service完成更新操作
        return  categoryService.update(category);
    }

    @DeleteMapping
    public R delete(Long id){
        return categoryService.delete(id);
    }


    // 通过type查询类别
    @GetMapping("list")
    public R findAllByType(Integer type){
        return categoryService.findAllByType(type);
    }

}
